import ezdxf
from ezdxf.addons import Importer
import random  # needed for random placing points

sdoc = ezdxf.readfile(r"C:\Users\YANG.LEI\Downloads\dxfs\999\1000x1000x1800(ke\06镜面6件.dxf")
tdoc = ezdxf.readfile(r"C:\Users\YANG.LEI\Downloads\line_rect_circle.dxf")
# tdoc = ezdxf.new()


importer = Importer(sdoc, tdoc)

# import all entities from source modelspace into modelspace of the target drawing
# importer.import_modelspace()
# import all paperspace layouts from source drawing
# importer.import_paperspace_layouts()

# import all CIRCLE and LINE entities from source modelspace into an arbitrary target layout.
# create target layout
tblock = tdoc.blocks.new('SOURCE_ENTS')
# tblock = tdoc.blocks.new('SOURCE_ENTS',base_point=(100,100))
# tblock.add_attdef('XPOS', (0.5, -4.0), dxfattribs={'height': 0.25, 'color': 4})
# tblock.add_attdef('YPOS', (1, -1.5), dxfattribs={'height': 0.25, 'color': 4})
# print(tblock.__dict__)
# print(tblock.entity_space.__dict__)
# query source entities
ents = sdoc.modelspace().query('CIRCLE LINE ARC')
print(ents)
# import source entities into target block
importer.import_entities(ents, tblock)

# This is ALWAYS the last & required step, without finalizing the target drawing is maybe invalid!
# This step imports all additional required table entries and block definitions.
importer.finalize()

msp = tdoc.modelspace()

msp.add_blockref('SOURCE_ENTS', (-550, -150), dxfattribs={
    'xscale': 1,
    'yscale': 1,
    'rotation': 0
})
# for flag_ref in msp.query('INSERT[name=="SOURCE_ENTS"]'):
#     flag_ref.explode()

tdoc.saveas('imported.dxf')

